package com.smart.village.big.screen.controller.bigscreen;

import com.alibaba.fastjson.JSONObject;
import com.smart.village.big.screen.domain.AjaxResult;
import com.smart.village.big.screen.domain.GridMember;
import com.smart.village.big.screen.service.GovernanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Logger;

import static com.smart.village.big.screen.domain.AjaxResult.error;
import static com.smart.village.big.screen.domain.AjaxResult.success;

/**
 * 基层治理数据接口
 */
@Tag(name = "基层治理数据接口")
@RestController
@RequestMapping("/governance")
public class GovernanceController {
    Logger logger = Logger.getLogger(GovernanceController.class.getName());

    @Autowired
    private GovernanceService governanceService;

    /**
     * 获取大屏 基层治理 网格化管理
     *
     * @return
     */
    @Operation(summary = "获取大屏 基层治理 网格化管理")
    @Parameters({
            @Parameter(name = "partyOrganizationSerialNumber", description = "下拉选择的最底级的党组织，默认传漠河市党委")
    })
    @GetMapping("/getGridManageStatic")
    @ResponseBody
    public AjaxResult getGridManageStatic(
            @RequestParam(required = false, defaultValue = "") String partyOrganizationSerialNumber) {
        try {
            JSONObject resultJson = governanceService.getGridManageStatic(partyOrganizationSerialNumber);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 基层治理 网格化员
     *
     * @return
     */
    @Operation(summary = "获取大屏 基层治理 网格化员")
    @Parameters({
            @Parameter(name = "partyOrganizationSerialNumber", description = "下拉选择的最底级的党组织，默认传漠河市党委")
    })
    @GetMapping("/getGridMember")
    @ResponseBody
    public AjaxResult getGridMember(
            @RequestParam(required = false, defaultValue = "") String partyOrganizationSerialNumber,
            @RequestParam String role
    ) {
        try {
            List<GridMember> gridManagers = governanceService.getGridMember(partyOrganizationSerialNumber, role);
            return success(gridManagers);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 根据 村镇 获取网格坐标列表
     *
     * @return
     */
    @Operation(summary = "数智治理 根据 村镇 获取网格坐标列表")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getGridInformationCoordsByVillage")
    @ResponseBody
    public AjaxResult getGridInformationCoordsByVillage(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber) {
        try {
            JSONObject resultJson = governanceService.getGridInformationCoordsByVillage(townNumber, villageNumber);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 根据 村镇 获取网格信息列表
     *
     * @return
     */
    @Operation(summary = "数智治理 根据 村镇 获取网格信息列表")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getGridInformationByVillage")
    @ResponseBody
    public AjaxResult getGridInformationByVillage(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(governanceService.getGridInformationByVillage(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 根据 村镇 获取网格信息列表
     *
     * @return
     */
    @Operation(summary = "数智治理 根据 村镇 根据网格id获取网格信息")
    @Parameters({
            @Parameter(name = "gridSerialNumber", description = "网格id")
    })
    @GetMapping("/getGridInformationByGridSerialNumber")
    @ResponseBody
    public AjaxResult getGridInformationByGridSerialNumber(
            @RequestParam String gridSerialNumber
    ) {
        try {
            return success(governanceService.getGridInformationByGridSerialNumber(gridSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 乡村基础数据
     *
     * @return
     */
    @Operation(summary = "数智治理 乡村基础数据")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getVillageBaseInformation")
    @ResponseBody
    public AjaxResult getVillageBaseInformation(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(governanceService.getVillageBaseInformation(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 村民共治 第一部分
     *
     * @return
     */
    @Operation(summary = "数智治理 村民共治 第一部分")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getVillagerCoGovernance")
    @ResponseBody
    public AjaxResult getVillagerCoGovernance(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(governanceService.getVillagerCoGovernance(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 村民共治 第二部分
     *
     * @return
     */
    @Operation(summary = "数智治理 村民共治 第二部分")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
            @Parameter(name = "searchType", description = "搜索类型"),
    })
    @GetMapping("/getVillagerCoGovernance2")
    @ResponseBody
    public AjaxResult getVillagerCoGovernance2(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam String searchType
    ) {
        try {
            List<JSONObject> villagerCoGovernance2 = governanceService.getVillagerCoGovernance2(townNumber, villageNumber, searchType);
            for (int i = 0; i < villagerCoGovernance2.size(); i++) {
                villagerCoGovernance2.get(i).put("rank", i + 1);
            }
            return success(villagerCoGovernance2);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 村民共治 第三部分
     *
     * @return
     */
    @Operation(summary = "数智治理 村民共治 第三部分")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
            @Parameter(name = "searchType", description = "搜索类型"),
    })
    @GetMapping("/getVillagerCoGovernance3")
    @ResponseBody
    public AjaxResult getVillagerCoGovernance3(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam String searchType
    ) {
        try {
            List<JSONObject> villagerCoGovernance3 = governanceService.getVillagerCoGovernance3(townNumber, villageNumber, searchType);
            for (int i = 0; i < villagerCoGovernance3.size(); i++) {
                villagerCoGovernance3.get(i).put("rank", i + 1);
            }
            return success(villagerCoGovernance3);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 村民共治 第三部分 详情
     *
     * @return
     */
    @Operation(summary = "数智治理 村民共治 第三部分 详情")
    @Parameters({

            @Parameter(name = "gridSerialNumber", description = "网格id"),
            @Parameter(name = "searchType", description = "搜索类型"),
    })
    @GetMapping("/getGridEventList")
    @ResponseBody
    public AjaxResult getGridEventList(
            @RequestParam String gridSerialNumber,
            @RequestParam String searchType
    ) {
        try {
            return success(governanceService.getGridEventList(gridSerialNumber, searchType));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 数字门牌 环形图 获取类型下拉
     *
     * @return
     */
    @Operation(summary = "数智治理 数字门牌 环形图 获取类型下拉")
    @GetMapping("/getDigitalDoorplateStaticsTagList")
    @ResponseBody
    public AjaxResult getDigitalDoorplateStaticsTagList(
    ) {
        try {
            return success(governanceService.getDigitalDoorplateStaticsTagList());
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 数字门牌 环形图
     *
     * @return
     */
    @Operation(summary = "数智治理 数字门牌 环形图")
    @Parameters({

            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
            @Parameter(name = "tagSerialNumber", description = "类型"),
    })
    @GetMapping("/getDigitalDoorplateStatics")
    @ResponseBody
    public AjaxResult getDigitalDoorplateStatics(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam String tagSerialNumber
    ) {
        try {
            return success(governanceService.getDigitalDoorplateStatics(townNumber, villageNumber, tagSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 综合治理 网格概况
     *
     * @return
     */
    @Operation(summary = "获取大屏 综合治理 网格概况")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
    })
    @GetMapping("/getComprehensiveGridManageStatic")
    @ResponseBody
    public AjaxResult getComprehensiveGridManageStatic(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber) {
        try {
            JSONObject resultJson = governanceService.getComprehensiveGridManageStatic(townNumber, villageNumber);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 综合治理 事件上报列表
     *
     * @return
     */
    @Operation(summary = "获取大屏 综合治理 事件上报列表")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
    })
    @GetMapping("/getComplaintsList")
    @ResponseBody
    public AjaxResult getComplaintsList(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            JSONObject resultJson = governanceService.getComplaintsList(townNumber, villageNumber, pageNum, pageSize);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 综合治理 网格概况
     *
     * @return
     */
    @Operation(summary = "获取大屏 综合治理 事件详情")
    @Parameters({
            @Parameter(name = "complaintId", description = "事件id 必传"),
    })
    @GetMapping("/getComplaintsDetail")
    @ResponseBody
    public AjaxResult getComplaintsDetail(
            @RequestParam String complaintId
    ) {
        try {
            JSONObject resultJson = governanceService.getComplaintsDetail(complaintId);
            return success(resultJson);
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 综合治理 事件现场预览
     *
     * @return
     */
    @Operation(summary = "获取大屏 综合治理 事件现场预览")
    @Parameters({
            @Parameter(name = "complaintId", description = "事件id 必传"),
    })
    @GetMapping("/getComplaintsDetailFullImage")
    @ResponseBody
    public AjaxResult getComplaintsDetailFullImage(
            @RequestParam String complaintId
    ) {
        try {
            return success(governanceService.getComplaintsDetailFullImage(complaintId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 综合治理 事件最新动态
     *
     * @return
     */
    @Operation(summary = "获取大屏 综合治理 事件最新动态")
    @Parameters({
            @Parameter(name = "complaintId", description = "事件id 必传"),
    })
    @GetMapping("/getComplaintsLastestDynamic")
    @ResponseBody
    public AjaxResult getComplaintsLastestDynamic(
            @RequestParam String complaintId
    ) {
        try {
            return success(governanceService.getComplaintsLastestDynamic(complaintId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 综合治理 事件进展情况
     *
     * @return
     */
    @Operation(summary = "获取大屏 综合治理 事件进展情况")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
    })
    @GetMapping("/getComplaintsLastProgress")
    @ResponseBody
    public AjaxResult getComplaintsLastProgress(
            @RequestParam(required = false) String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(governanceService.getComplaintsLastProgress(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 综合治理 边防补助
     *
     * @return
     */
    @Operation(summary = "获取大屏 综合治理 边防补助")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传"),
    })
    @GetMapping("/getPatrolCheckIn")
    @ResponseBody
    public AjaxResult getPatrolCheckIn(
            @RequestParam String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(governanceService.getPatrolCheckIn(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 数智治理 综合治理 巡查走访情况
     *
     * @return
     */
    @Operation(summary = "数智治理 综合治理 巡查走访情况")
    @Parameters({
            @Parameter(name = "townNumber", description = "镇编码 必传"),
            @Parameter(name = "villageNumber", description = "村编码 非必传")
    })
    @GetMapping("/getInspectionVisitSituation")
    @ResponseBody
    public AjaxResult getInspectionVisitSituation(
            @RequestParam String townNumber,
            @RequestParam(required = false, defaultValue = "") String villageNumber
    ) {
        try {
            return success(governanceService.getInspectionVisitSituation(townNumber, villageNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }
}
